package hbr;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * This tests the firing for the robot KillBot.
 * 
 * @author Bryson Y. Hori
 */
public class TestFiring extends RobotTestBed {
  boolean firePowerLevel = false;
  double powerLast;
  
  /**
   * Specifies that Tracker and KillBot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Tracker, hbr.KillBot";
  }
  
  /**
   * This test runs for 25 rounds.
   * 
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 25;
  }
  
  /**
   * At the end of each turn, checks the power of all bullets moving across the 
   * battlefield. Checks to see if there is change between value of the bullets fired, proving
   * that KillBot is using proportional firing based on distance of detected enemy robot.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded (TurnEndedEvent event) {
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();
    double bulletPower;
    
    for (int i = 0; i < bullets.length; i++) {
      if (i == 0) {
        bulletPower = bullets[i].getPower();
        powerLast = bulletPower;
      }
      else {
        bulletPower = bullets[i].getPower();
        if (bulletPower != powerLast) {
          firePowerLevel = true;
        }
        powerLast = bulletPower;
      }
    }
  }
  
  /**
   * After running all matches, checks to see if there is any change in KillBots bullet power.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted (BattleCompletedEvent event) {
    assertTrue("Bullet Power has changed", firePowerLevel);
  }
}
